// pages/view/cl/volumeAnalysis/volumeAnalysis.js
//var wxCharts = require('../../utils/wxcharts.js');
const network = require('../../../../utils/network.js')
var app = getApp();
var columnChart = null;
var pieChart = null;
var lineChart = null;

Page({
  data: {

    chartType: 1,

    changeFlag_dept : true,
    changeFlag_gender : false,
    changeFlag_date : false,

    //背景颜色
    deptBg: '#37a2da',
    genderBg: '#C1C1C1',
    dateBg: '#C1C1C1',


    
    chartTitle_1: '门诊诊疗人次-科室分析',
    chartTitle_2:'门诊诊疗人次-性别分析',
    chartTitle_3: '门诊诊疗人次-日期分析',

    isMainChartDisplay: true,


    dept_title_1: '科室名称',
    dept_title_2: '门诊人次',
    dept_data_list:[
      {
        deptName: '妇产科门诊',
        clTimes: '19'
      },
      {
        deptName: '儿保科门诊',
        clTimes: '19'
      },
      {
        deptName: '住院妇产科',
        clTimes: '19'
      }
    ],
    dept_totalTimes: 57,

    dept_categories: [],
    dept_data:[],




    gender_title_1: '性别',
    gender_title_2: '人次',
    gender_data_list:[
      {
        rowName: '男',
        clTimes: '49'
      },
     {
       rowName: '女',
       clTimes: '103'
     }
    ],
    gender_totalTimes: 152,

    gender_man_data: 0,
    gender_women_data: 0,
    gender_unknow_data: 0,



    date_title_1: '日期',
    date_title_2: '人次',
    date_data_list:[
      {
        rowName: '2018-01-01',
        clTimes: '32'
      },
      {
        rowName: '2018-01-02',
        clTimes: '54'
      },
      {
        rowName: '2018-01-03',
        clTimes: '49'
      },
      {
        rowName: '2018-01-04',
        clTimes: '56'
      }
    ],
    date_totalTimes: 191,

    date_list:[],
    date_data:[]



  
  },
  onLoad: function (options){
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    that.myRequest(options)

  },

  /**
 * 调用封装好的网络请求，获取数据
 */
  myRequest: function (options) {
    var that = this;
    console.log('options:', options)
    //调用封装好的网络请求，获取数据
    network.get('/cl/volumeAnalysis?startTime=' + options.startTime + '&endTime=' + options.endTime, {}).then(function (res) {
      console.log('res:', res)
      var obj1_chart_dept = res.data.data.list2
      var obj1_chart_data = res.data.data.list3
      var obj1_table = res.data.data.list1
      var obj2_table = res.data.data_2
      var obj3_chart_date = res.data.data_3.list2
      var obj3_chart_data = res.data.data_3.list3
      var obj3_table = res.data.data_3.list1
      console.log("obj1_chart_dept:", obj1_chart_dept)
      console.log("obj1_chart_data:", obj1_chart_data)
      console.log("obj1_table:", obj1_table)
      console.log("obj2_table:", obj2_table)
      console.log("obj3_table:", obj3_table)
      that.setData({
        'dept_data_list': obj1_table,
        'gender_data_list': obj2_table,
        'date_data_list': obj3_table,

        'dept_categories': obj1_chart_dept,
        'dept_data': obj1_chart_data,

        'date_list': obj3_chart_date,
        'date_data': obj3_chart_data
      })
      if (obj2_table.length >= 3) {
        that.setData({
          'gender_man_data': obj2_table[0].clTimes,
          'gender_women_data': obj2_table[1].clTimes,
          'gender_unknow_data': obj2_table[2].clTimes,
        })
      } else {
        that.setData({
          'gender_man_data': obj2_table[0].clTimes,
          'gender_women_data': obj2_table[1].clTimes,
        })
      }


      var dept_totalCount = 0;
      var gender_totalCount = 0;
      var date_totalCount = 0;
      for (var i = 0; i < obj1_table.length; i++) {
        dept_totalCount += obj1_table[i].clTimes;
      }
      for (var i = 0; i < obj2_table.length; i++) {
        gender_totalCount += obj2_table[i].clTimes;
      }
      for (var i = 0; i < obj3_table.length; i++) {
        date_totalCount += obj3_table[i].clTimes;
      }

      that.setData({
        'dept_totalTimes': dept_totalCount,
        'gender_totalTimes': gender_totalCount,
        'date_totalTimes': date_totalCount
      })
      console.log("dept_totalCount", dept_totalCount)


    }).then(function (res) {
      wx.hideLoading()
      that.createColumnChart();

    }).catch(function (res) {
      console.log(res)
    })
  },

  createColumnChart: function (e) {
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: that.data.dept_categories,
      series: [{
        name: '门诊人次',
        data: that.data.dept_data,
        format: function (val, name) {
        
          return val.toFixed(2) + '个';
        }
      }],
      yAxis: {
        format: function (val) {
          return val + '';
        },
        title: '门诊人次',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 10
        }
      },
      width: windowWidth,
      height: 300,
    });
  },

   createPieChart: function (e) {
     var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'pie',
      series: [{
        name: '男',
        data: that.data.gender_man_data,
      }, {
        name: '女',
          data: that.data.gender_women_data,
      },
        {
      
          name: '未知性别者',
          data: that.data.gender_unknow_data,
        }
      ],
      width: windowWidth,
      height: 300,
      dataLabel: true,
    });
  },


  createLineChart:function(){
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

   // var simulationData = this.createSimulationData();
    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: that.data.date_list,
      animation: true,
      // background: '#f5f5f5',
      series: [{
        name: '门诊人次',
        data: that.data.date_data,
        format: function (val, name) {
          return val.toFixed(2) + '';
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '门诊人次',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: windowWidth,
      height: 300,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  changeToDept: function(e){
    var that = this;
    var flag = that.data.changeFlag_dept;
    console.log('flag:',flag)
    if (!flag){
      that.data.changeFlag_dept = true
      that.setData({
        'changeFlag_dept':  true,
        'changeFlag_gender': false,
        'changeFlag_date':false,
        'deptBg': "#37a2da",
        'genderBg': "#C1C1C1",
        'dateBg': "#C1C1C1",
        chartType:1
      })
      
       that.createColumnChart();
     

     
    }
   

  },
  changeToGender: function (e) {
    var that = this;
    var flag = that.data.changeFlag_gender;
    if (!flag){
      that.setData({
        'changeFlag_dept': false,
        'changeFlag_gender': true,
        'changeFlag_date': false,
        'deptBg': "#C1C1C1",
        'genderBg': "#32c5e9",
        'dateBg': "#C1C1C1",
        chartType: 2
      })
      that.createPieChart();
     
    }
   

  },
  changeToDate: function (e) {
    var that = this;
    var flag = that.data.changeFlag_date;
    if (!flag){
      that.setData({
        'changeFlag_dept': false,
        'changeFlag_gender': false,
        'changeFlag_date': true,
        'deptBg': "#C1C1C1",
        'genderBg': "#C1C1C1",
        'dateBg': "#67e0e3",
        chartType: 3
      })
      that.createLineChart();
    }
    

  }

});